<%@page import="study.top.mydb.PaymentP"%>
<%@page import="study.top.mydb.items.PaymentInfoItem"%>
<%@page import="study.top.mydb.PaymentInfo"%>
<%@page import="study.top.mydb.items.WishListShowItem"%>
<%@page import="study.top.mydb.WishListProduct"%>
<%@page import="study.top.common.PatternChecker"%>
<%@page import="study.top.mydb.Member"%>
<%@page import="study.top.jsp.WebHandler"%>
<%@page import="study.top.mydb.Product"%>
<%@page import="study.top.mydb.items.ProductItem"%>
<%@page import="study.top.mydb.items.MemberItem"%>
<%@page import="study.top.mydb.WishList"%>
<%@page import="study.top.mydb.items.WishListItem"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page session="true"%>
<%@ include file="dbcon.jsp"%>
<!DOCTYPE html>
<html lang="ko">
<%@include file="topHead.jsp"%>

<body>
	<%@include file="topcategory.jsp"%>

	<div class="top-tab top-hidden-xs">
		<%@include file="tab.jsp"%>
	</div>
	<%
		db.connect();

		WishListProduct w = new WishListProduct();
		ArrayList<WishListShowItem> wishlist_list = w.wishListMember(mem_seq);
		
		PaymentInfo p = new PaymentInfo();
		//사용자가 가지고있는 적립금 가져오기
		int allPoint=p.allPoint(mem_seq);
		//체크박스에서 가져온 seq를 배열로 받아옴
		String[] seq1 = request.getParameterValues("chkbox");
		if (seq1 == null) {
			out.print("<script type='text/javascript'>alert('선택된 상품이 없습니다.');history.back();</script>");
			return;
		}
	%>
	<form name="form1" role="form" method="post"
		action="uPayOk.jsp">

		<div class="container">
			<div class="page-header wishmargin">
				<h2>
					<strong>PROCEED TO CHECKOUT</strong>
				</h2>
			</div>
			<div class="Info wishmargin">
				<p>회원님의 결제페이지 입니다.</p>
				<br /> <br />
			</div>
			<div class="table-wishList">
				<div class="top-line2">
					<div class="top-tr1">
						&nbsp;&nbsp;&nbsp;
						<div class="top-td-forth top-hidden-xs">
							<strong>구분</strong>
						</div>
						<div class="top-td-fir">
							<strong>상품명</strong>
						</div>
						<div class="top-td-forth top-hidden-xs">
							<strong>우선순위</strong>
						</div>
						<div class="top-td-third">
							<strong>판매가</strong>
						</div>
						<div class="top-td-third">
							<strong>수량</strong>
						</div>
						<div class="top-td-third top-hidden-xs">
							<strong>적립금</strong>
						</div>
						<div class="top-td-third">
							<strong>합계</strong>
						</div>

					</div>
				</div>
				<%
					//주문할 상품 합계금액
					int paysum=0;
					//위시리스트 넘버
					int no=0;
					//순수 쌓이게되는 적립금<합계금액*0.05>
					int savepoint=0;
					//배열을 화면에 나타내기위해 for문으로 돌린다.
					for (int i = 0; i < seq1.length; i++) {
					int seq2 = Integer.parseInt(seq1[i]);
						ArrayList<WishListShowItem> pay_list = w.payment(seq2);
						for (int j = 0; j < pay_list.size(); j++) {
							WishListShowItem item = pay_list.get(j);
							paysum += item.getSum();
							savepoint+=item.getPoint();
							no=item.getWishList_seq();

				%>
				<input type="hidden" name="paysum" value="<%=paysum%>" /> <input
					type="hidden" name="savepoint" value="<%=savepoint%>" />

				<div class="top-tr1 data-line">
					&nbsp;&nbsp;&nbsp;
					<div class="top-td-forth top-hidden-xs">
						<strong>기본배송</strong>
					</div>
					<div class="top-td-fir top-text-inline">
						<%=item.getTitle()%>
					</div>
					<div class="top-td-forth top-hidden-xs"><%=item.getRank()%></div>
					<div class="top-td-third">
						<span class="price"> <%=item.getPrice_min()%></span> 원
					</div>
					<div class="top-td-third">
						<%=item.getQty()%>
					</div>
					<div class="top-td-third top-hidden-xs">
						<%=item.getPoint()%>
						원&nbsp;&nbsp;&nbsp;
					</div>
					<div class="top-td-third">
						<%=item.getSum()%>
					</div>
					<div class="top-td-sixth">
						<input type="hidden" name="chkbox1" value="<%=item.getSeq()%>" />
					</div>
				</div>
				<%
					}
																																																															}
				%>
			</div>
		</div>


		<div class="table-wishList1 ">
			<div class="margin">
				<div class="td-sum">총 주문 합계 금액입니다.</div>
				<div class="td-sum top-hidden-xs"></div>
				<div class="td-sum top-hidden-xs"></div>
				<div class="td-sum">
					<h5>
						상품 합계금액 :&nbsp;&nbsp;&nbsp;&nbsp; <span class="sum"> <%=paysum%></span>
						원
					</h5>
					<h5>회원할인금액 :</h5>
					<h5>
						총 주문합계 금액 :
						<%=paysum%>
						원
					</h5>
				</div>
			</div>
		</div>
		<%
				Member member = new Member();
				MemberItem mem_item= member.show(mem_seq);	
				DBHandler.getInstance().disconnect();
		%>
		<div class="container">
			<h3 class="wishmargin">&nbsp;배송지정보</h3>
			<br />
			<div class="table-wishList">
				<div class="top-tr1 top-line2">
					<div class="top-td-sec">
						<strong>성명</strong><br />
					</div>
					<div class="top-td">
						&nbsp;&nbsp;&nbsp; <input type="text" class="top-input" id="name"
							value="<%=mem_item.getName()%>" name="name">
					</div>
				</div>

				<div class="top-tr1">
					<div class="top-td-sec"></div>
					<div class="top-td">
						&nbsp;&nbsp;&nbsp; <input type="text" class="top-input"
							value="<%=mem_item.getZipcode() %>" id="zipcode"
							name="zipcode"> <input type="button"
							class="top-sm-text top-btn-size1 top-btn-custom" name="zip_check"
							value="우편번호찾기">


					</div>
				</div>
				<div class="top-tr1">
					<div class="top-td-sec">
						<strong>주소</strong>
					</div>
					<div class="top-td">
						&nbsp;&nbsp;&nbsp; <input type="text" class="top-input" id="add1"
							value="<%=mem_item.getAdd1() %>" name="add1"> <strong>기본주소</strong>
					</div>
				</div>
				<div class="top-tr1 top-line2">
					<div class="top-td-sec"></div>
					<div class="top-td">
						&nbsp;&nbsp;&nbsp; <input type="text" class="top-input" id="add2"
							value="<%=mem_item.getAdd2() %>" name="add2"> <strong>나머지주소</strong>
					</div>
				</div>
				<div class="top-tr1 top-line2">
					<div class="top-td-sec">
						<strong>연락처</strong>
					</div>
					<div class="top-td">
						&nbsp;&nbsp;&nbsp; <input type="tel" class=top-input id="tel"
							placeholder="연락처를 입력하세요" name="tel">
					</div>
				</div>

				<div class="top-tr1">
					<div class="top-td-sec">
						<strong>배송메시지</strong>
					</div>
					<div class="top-td">
						&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" class=top-input2
							id="message" placeholder="메시지를 입력해주세요" name="message">

					</div>
				</div>
			</div>
		</div>


		<br /> <br />
		<div class="container ">
			<h3 class="wishmargin">&nbsp;결제&nbsp;정보</h3>
			<br />
			<div class="table-wishList">
				<div class="top-tr1 top-line2">
					<div class="top-td-sec">
						<Strong>적립금 사용</Strong>
					</div>
					<div class="top-td">
						&nbsp;&nbsp;&nbsp; <input name="usepoint" class="usepoint"
							type="number" value="0" min="0" max="<%=allPoint%>"> 원 (총
						사용가능 적립금 : <span class="sumpoint"><%=allPoint%></span> 원)
					</div>
				</div>
				<div class="top-tr1">
					<div class="top-td-sec">
						<strong>결제금액</strong>
					</div>
					<div class="top-td">
						<label for="name" class="top-label">&nbsp;&nbsp;&nbsp;</label> <span
							class="total_pay"> <%=paysum%></span> 원
					</div>
				</div>
			</div>

		</div>
		<br /> <br />
		<div class="container top-center">

			<input type="submit" id="payment"
				class="top-sm-text top-btn-size1 top-btn-custom" value="결제하기">

		</div>
	</form>
	<br />
	<br />
	<div class="top-hidden-xs">
		<%@include file="topBottom.jsp"%>
	</div>
	<!-- Javascript -->
	<script src="assets/js/string.js"></script>
	<script type="text/javascript">
		$(function() {
			$("input[name='zip_check']")
					.click(
							function() {
								// 우편번호 입력여부 검사
								if (!$("input[name='zipcode']").val().isValue()) {
									alert("동이름을 입력해주세요.");
									$("input[name='zipcode']").focus();
									return false;
								}

								$.ajax({
											url : "ajax/zipcode.jsp",
											data : "dong="
													+ $("input[name='zipcode']")
															.val(),
											dataType : "text",
											timeout : 30000,
											cache : false,
											success : function(req) {
												if (req) {
													var zip = req.substring(0,
															7);
													var add1 = req.substring(8,
															req.length);
													alert("상세주소를 입력해주세요.");
													document
															.getElementById('zipcode').value = zip;
													document
															.getElementById('add1').value = add1;
												} else {
													alert("검색된 결과가 없습니다.\n직접 입력해 주세요.");
													document
															.getElementById('zipcode').value = "";
													document
															.getElementById('add1').value = "";
												}
											},
											error : function(xhr, status, error) {
												alert("status: " + status
														+ " / error-code: "
														+ xhr.status + " "
														+ error);
											}
										});
							});
			//사용할 포인트 지정시
			$(".usepoint").change(function() {
				// 포인트
				var point = $(this).parents().find(".sumpoint").html();

				if (point) {
					point = parseInt(point);
				} else {
					point = 0;
				}
				// 사용할 포인트
				var usepoint = $(this).parent().find(this).val();

				// 전체 금액 합계 구하기
				if (usepoint) {
					usepoint = parseInt(usepoint);

				} else {
					usepoint = 0;
				}

				if (usepoint > point) {
					alert("적립금보다 많이쓰셨습니다.");
					var usepoint = $(this).parent().find(this).val(0);
					return;
				}

				//전체금액
				var sum = $(this).parents().find(".sum").html();
				if (sum) {
					sum = parseInt(sum);
				} else {
					sum = 0;
				}
				$(this).parents().find(".sum").html(sum);
				var sumpay = sum - usepoint;
				//	var parampoint=point-usepoint;

				sumpay = $(".total_pay").html(sumpay);
				// 결제금액
				var pay = $(this).parents().find(".total_pay").html();
				if (pay) {
					pay = parseInt(pay);
				} else {
					pay = 0;
				}

			});

			$("form[name='form1']").submit(function() {
				// 이름 입력여부 검사
				if (!$("input[name='name']").val().isValue()) {
					alert("이름을 입력하세요.");
					$("input[name='name']").focus();
					return false;
				}

				// 우편번호 입력여부 검사
				if (!$("input[name='zipcode']").val().isValue()) {
					alert("동이름을 입력해주세요.");
					$("input[name='zipcode']").focus();
					return false;
				}
				// 주소1 형식 검사
				if (!$("input[name='add1']").val().isValue()) {
					alert("주소1을 입력해주세요.");
					$("input[name='add1']").focus();
					return false;
				}

				// 주소2 형식 검사
				if (!$("input[name='add2']").val().isValue()) {
					alert("주소2를 입력해주세요.");
					$("input[name='add2']").focus();
					return false;
				}

				// 전화번호 입력여부 검사
				if (!$("input[name='tel']").val().isValue()) {
					alert("전화번호를 입력하세요.");
					$("input[name='tel']").focus();
					return false;
				}

			});
			$("#payment").click(function() {
				$("input[name='chkbox1']").attr("checked", "checked");
				if (!confirm("결제 하시겠습니까?")) {
					return false;
				}
			});
		});
	</script>
</body>
</html>

